<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\approval;

use think\facade\Db;
use app\common\repositories\BaseRepository;
use app\common\dao\approval\ApprovalFlowDao;
use app\common\repositories\work\WorkExpendApplyRepository;
use app\common\repositories\property\users\PropertyUsersLogRepository;

/**
 * Class ApprovalFlowRepository
 * @package app\common\repositories\poster
 * @mixin ApprovalFlowDao
 */
class ApprovalFlowRepository extends BaseRepository
{

    public function __construct(ApprovalFlowDao $dao)
    {
        $this->dao = $dao;
    }

    public function getPropertyList(array $where, $page, $limit, $propertyId)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->with([
                'submitUserInfo' => function ($query) {
                    $query->bind(['submit_username' => 'username']);
                },
                'presentUserInfo' => function ($query) {
                    $query->bind(['present_username' => 'username']);
                }
            ])
            ->order('id', 'desc')
            ->select();
        /** @var ApprovalConfigRepository $configRepository */
        $configRepository = app()->make(ApprovalConfigRepository::class);
        $isTypeData = $configRepository::IS_TYPE;
        $flowTypeBData = $configRepository::FLOW_TYPE_B;
        $flowTypeCData = $configRepository::FLOW_TYPE_C;
        $flowTypeData = array_merge($flowTypeBData, $flowTypeCData);
        foreach ($list as $k => $v) {
            $list[$k]['flow_name'] = ($v['submit_username'] ?? '') . ' 申请的 ' . ($flowTypeData[$v['flow_type']] ?? '') . ($isTypeData[$v['is_type']] ?? '');
            $list[$k]['flow_type'] = ($flowTypeData[$v['flow_type']] ?? '');
        }
        return compact('count', 'list');
    }

    public function addInfo(array $data, int $propertyId = 1, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $propertyId, $userId) {
            $data['property_id'] = $propertyId;
            $res = $this->dao->create($data);

            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($propertyId, $userId, 'APPROVAL.FLOW.ADD', $data);
            return $res;
        });
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $info, $userId) {
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'APPROVAL.FLOW.EDIT', $data);
            return $this->dao->update($info['id'], $data);
        });
    }

    public function getDetail(int $id)
    {
        return $this->dao->getSearch(['id' => $id])
            ->find();
    }

    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }


    public function deleteInfo($info, int $userId = 0)
    {
        if ($info['flow_type'] == 'expend_apply') {
            ## 报销流程
            /** @var WorkExpendApplyRepository $workExpendApplyRepository */
            $workExpendApplyRepository = app()->make(WorkExpendApplyRepository::class);
            $delInfo = $workExpendApplyRepository->get($info['source_id']);
            $workExpendApplyRepository->deleteInfo($delInfo, $userId);
        }
        if ($info['flow_type'] == 'for_leave') {
            ## 请假流程

        }
    }

}